using System.Collections;
using System.Collections.Generic;
using Net.Common;
using UnityEngine;

/****************************************************
// 功能：模拟战斗场景
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/27 16:15:8
*****************************************************/

public class SimulateController : SingletonPatternMonoBase<SimulateController>
{
    public bool isActive = false;

    private uint frameId = 0;
    private readonly List<OpKey> opKeyList = new List<OpKey>();

    public void StartSimulate()
    {
        isActive = true;
        if (Camera.main != null) Camera.main.gameObject.SetActive(false);
        StartCoroutine(BattleSimulate());
    }
    
    private IEnumerator BattleSimulate() {
        SimulateLoadResource();
        yield return new WaitForSeconds(0.5f);
        SimulateBattleStart();
    }
    
    private void SimulateLoadResource() {
        var msgInfo = new MsgInfo()
        {
            requestType = RequestType.SELECT_HERO,
            data = "{\"mapId\":101, \"battleHero\":[" +
                   "{\"heroId\":101, \"userName\":  \"独断万古荒天帝\"},  {\"heroId\":102, \"userName\":  \"落霞神女\"}]}",
        };
        ModelManager.Instance.currentIndex = 0;
        HeroController.Instance.OnLoadBattleHero(msgInfo);
    }

    private void SimulateBattleStart()
    {
        BattleController.Instance.OnBattleStart(new MsgInfo());
    }

    public void SimulateServerMsg(MsgInfo msgInfo)
    {
        var send = JsonUtility.FromJson<SendOpKeyRequest>(msgInfo.data);
        UpdateOpKey(send.opKey);
    }

    private void UpdateOpKey(OpKey key)
    {
        opKeyList.Add(key);
    }


    private void FixedUpdate()
    {
        if (!isActive) { return; }
        
        ++frameId;
        var ntfOpKey = new NtfOpKey()
        {
            frameId = frameId,
            keyList = new List<OpKey>()
        };

        var count = opKeyList.Count;
        if (count > 0)
        {
            foreach (var key in opKeyList)
            {
                ntfOpKey.keyList.Add(key);
            }
        }

        opKeyList.Clear();
        ClientTcp.Instance.HandleTcpMsgInfo(RequestType.SEND_OP_KEY, JsonUtility.ToJson(ntfOpKey));
    }
}
